/*
Copyright © 2023 WangLei. All rights reserved.
自定义指令，支持组件可移动
@file drag.js
@author: WangLei
@date: 15:16:41, 九月 27, 2023
@license: MIT
*/

// 其他实现方式参数：https://hu-snail.github.io/vue3-resource/platform/web/drag.html

// 方式一：参数为对象
export default (app) => {
  app.directive('dragMove', (el, binding) => {
    // body当前宽度
    const screenWidth = document.body.clientWidth
    // body高度
    const screenHeight = document.documentElement.clientHeight
    // 拖拽按钮
    const DragButton = el.querySelector(binding.value.DragButton)
    DragButton.style.cssText += ';cursor:move;'
    // 拖拽窗口 DragVindow
    // 拖拽窗口的 position 属性要设置为fixed
    const DragVindow = el
    // 如果是自定义组件 设置窗口默认居中
    if (binding.value.custom) {
      const [left, top] = [
        screenWidth - DragVindow.offsetWidth,
        screenHeight - DragVindow.offsetHeight
      ]
      DragVindow.style.cssText += `;left:${left / 2}px;top:${top / 2}px;`
    }
    const sty = (function () {
      if (window.document.currentStyle) {
        return (dom, attr) => dom.currentStyle[attr]
      } else {
        return (dom, attr) => getComputedStyle(dom, false)[attr]
      }
    })()

    // 按下鼠标处理事件
    DragButton.onmousedown = (e) => {
      // 鼠标按下，计算当前元素距离可视区的距离
      const disX = e.clientX - DragButton.offsetLeft
      const disY = e.clientY - DragButton.offsetTop

      const dragDomWidth = DragVindow.offsetWidth // 对话框宽度
      const dragDomheight = DragVindow.offsetHeight // 对话框高度

      const minDragDomLeft = DragVindow.offsetLeft
      const maxDragDomLeft = screenWidth - DragVindow.offsetLeft - dragDomWidth

      const minDragDomTop = DragVindow.offsetTop
      const maxDragDomTop = screenHeight - DragVindow.offsetTop - dragDomheight

      let styL = sty(DragVindow, 'left')
      let styT = sty(DragVindow, 'top')
      if (styL.includes('%')) {
        styL = +document.body.clientWidth * (+styL.replace(/%/g, '') / 100)
        styT = +document.body.clientHeight * (+styT.replace(/%/g, '') / 100)
      } else {
        styL = +styL.replace(/px/g, '')
        styT = +styT.replace(/px/g, '')
      }
      document.onmousemove = (e) => {
        // 通过事件委托，计算移动的距离
        let left = e.clientX - disX
        let top = e.clientY - disY

        // 边界处理
        // if (-left > minDragDomLeft) {
        //   left = -minDragDomLeft
        // } else if (left > maxDragDomLeft) {
        //   left = maxDragDomLeft
        // }

        // if (-top > minDragDomTop) {
        //   top = -minDragDomTop
        // } else if (top > maxDragDomTop) {
        //   top = maxDragDomTop
        // }
        // 设置当前元素
        DragVindow.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`
      }
      document.onmouseup = () => {
        document.onmousemove = null
        document.onmouseup = null
      }
    }

    //双击鼠标，复位
    DragButton.ondblclick = (e) => {
      DragVindow.style.cssText += `;left:50px;top:50px;`
    }
  })
}

//方式二：参数为函数
// export default (app) => {
//   //自定义组件
//   app.directive('drag', (el, binding) => {
//     el.onmousedown = function (e) {
//       var distX = e.pageX - el.offsetLeft
//       var distY = e.pageY - el.offsetTop
//       console.log('元素本身的高：' + el.clientHeight + ',元素本身的宽：' + el.clientWidth)

//       if (e.preventDefault) {
//         e.preventDefault()
//       } else {
//         e.returnValue = false
//       } //解决快速拖动滞后问题

//       document.onmousemove = function (e) {
//         // 用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
//         let left = e.clientX - distX
//         let top = e.clientY - distY

//         if (left <= 0) {
//           left = 5 //设置成5是为了不离边缘太近
//         } else if (left > document.documentElement.clientWidth - el.clientWidth) {
//           //document.documentElement.clientWidth 屏幕的可视宽度
//           left = document.documentElement.clientWidth - el.clientWidth - 5
//         }
//         if (top <= 0) {
//           top = 5
//         } else if (top > document.documentElement.clientHeight - el.clientHeight) {
//           top = document.documentElement.clientHeight - el.clientHeight - 5
//         }
//         el.style.left = left + 'px'
//         el.style.top = top + 'px'
//       }
//       document.onmouseup = function () {
//         document.onmousemove = document.onmouseup = null
//       }
//     }
//   })
// }
